package com.aiwei.tdjk.controller.sys;

import com.aiwei.tdjk.common.Constants;
import com.aiwei.tdjk.controller.BaseController;
import com.aiwei.tdjk.dto.request.GetRolePagerRequest;
import com.aiwei.tdjk.entity.Role;
import com.aiwei.tdjk.entity.RoleResourcePermission;
import com.aiwei.tdjk.enums.AvailableEnum;
import com.aiwei.tdjk.service.PermissionService;
import com.aiwei.tdjk.service.RoleResourcePermissionService;
import com.aiwei.tdjk.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;


@Controller
@RequestMapping(value = "/admin/sys/permission/role")
public class RoleController extends BaseController<Role> {

    @Autowired
    private RoleService roleService;

    @Autowired
    private PermissionService permissionService;

    @Autowired
    private RoleResourcePermissionService roleResourcePermissionService;


    protected void setCommonData(Model model) {
        model.addAttribute("availableList", AvailableEnum.values());
    }


    @RequestMapping(value = {""}, method = RequestMethod.GET)
    public String list(GetRolePagerRequest request,Model model) {

        setCommonData(model);
        model.addAttribute("page", roleService.find(request));
        model.addAttribute("m", request);
        return viewName("list");
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public String view(Model model, @PathVariable("id") Long id) {
        setCommonData(model);
        model.addAttribute("m", roleService.get(id));
        List<RoleResourcePermission> resourcePermissions = roleResourcePermissionService.getResourcePermissionsByRoleId(id);
        model.addAttribute("resourcePermissions",resourcePermissions);
        model.addAttribute(Constants.OP_NAME, "查看");
        return viewName("editForm");
    }


    @RequestMapping(value = "create", method = RequestMethod.GET)
    public String showCreateForm(Model model) {

        setCommonData(model);
        model.addAttribute(Constants.OP_NAME, "新增");
        if (!model.containsAttribute("m")) {
            model.addAttribute("m", new Role());
        }
        model.addAttribute("permissions", permissionService.list());
        return viewName("editForm");
    }


    @RequestMapping(value = "create", method = RequestMethod.POST)
    public String create(
            Model model, @Valid @ModelAttribute("m") Role role,Long[] resourceId,String[] permissionIds, BindingResult result,
            RedirectAttributes redirectAttributes) {

        if (hasError(result)) {
            return showCreateForm(model);
        }
        roleService.save(role,resourceId,permissionIds);
        redirectAttributes.addFlashAttribute(Constants.MESSAGE, "新增成功");
        return redirectToUrl("");
    }


    @RequestMapping(value = "{id}/update", method = RequestMethod.GET)
    public String showUpdateForm(@PathVariable("id") Long id, Model model) {
        setCommonData(model);
        model.addAttribute(Constants.OP_NAME, "修改");
        model.addAttribute("m", roleService.get(id));

        model.addAttribute("permissions", permissionService.list());
        List<RoleResourcePermission> resourcePermissions = roleResourcePermissionService.getResourcePermissionsByRoleId(id);
        model.addAttribute("resourcePermissions",resourcePermissions);
        return viewName("editForm");
    }


    @RequestMapping(value = "{id}/update", method = RequestMethod.POST)
    public String update(
            Model model, @Valid @ModelAttribute("m") Role m, Long[] resourceId, String[] permissionIds, BindingResult result,
            @RequestParam(value = Constants.BACK_URL, required = false) String backURL,
            RedirectAttributes redirectAttributes) {

        if (hasError(result)) {
            return showUpdateForm(m.getId(), model);
        }
        roleService.update(m,resourceId,permissionIds);
        redirectAttributes.addFlashAttribute(Constants.MESSAGE, "修改成功");
        return redirectToUrl("");
    }

    @RequestMapping(value = "{id}/delete", method = RequestMethod.GET)
    public String showDeleteForm(@PathVariable("id") long id, Model model) {

        setCommonData(model);
        model.addAttribute(Constants.OP_NAME, "删除");
        model.addAttribute("m", roleService.get(id));
        List<RoleResourcePermission> resourcePermissions = roleResourcePermissionService.getResourcePermissionsByRoleId(id);
        model.addAttribute("resourcePermissions",resourcePermissions);
        return viewName("editForm");
    }

    @RequestMapping(value = "{id}/delete", method = RequestMethod.POST)
    public String delete(
            @PathVariable("id") long id,
            RedirectAttributes redirectAttributes) {

        roleService.delete(id);

        redirectAttributes.addFlashAttribute(Constants.MESSAGE, "删除成功");
        return redirectToUrl("");
    }

    @RequestMapping(value = "batch/delete", method = {RequestMethod.GET, RequestMethod.POST})
    public String deleteInBatch(
            @RequestParam(value = "ids", required = false) Long[] ids,
            RedirectAttributes redirectAttributes) {

        roleService.delete(ids);
        redirectAttributes.addFlashAttribute(Constants.MESSAGE, "删除成功");
        return redirectToUrl("");
    }

    @RequestMapping(value = "/changeStatus/{newStatus}")
    public String changeStatus(
            HttpServletRequest request,
            @PathVariable("newStatus") Boolean newStatus,
            @RequestParam("ids") Long[] ids,
            RedirectAttributes redirectAttributes) {


        for (Long id : ids) {
            Role role = roleService.get(id);
            role.setIsShow(newStatus);
            roleService.update(role);
        }

        redirectAttributes.addFlashAttribute(Constants.MESSAGE, "操作成功！");

        return redirectToUrl("");
    }


    @RequestMapping("{id}/permissions")
    public String permissions(@PathVariable("id") Long id,Model model) {
        List<RoleResourcePermission> resourcePermissions = roleResourcePermissionService.getResourcePermissionsByRoleId(id);
        model.addAttribute("resourcePermissions",resourcePermissions);
        return viewName("permissionsTable");
    }

}
